﻿/******************************************************************************
* File description: execonfig.cpp
* Function: 实验配置、默认配置
* Current Version: 1.1
* Author: 陈乃瑞
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
#include "execonfig.h"

ExeConfig::ExeConfig()
{

}
/**
 * @brief 数据库读某一实验流程
 * @param 实验id
 * @return 实验流程列表
 */
QJsonArray ExeConfig::readsypz(int id)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type,value_1 From exe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList stepval;
    QStringList typeval;
    QStringList value_1val;
    QStringList colorval;
    QStringList pathval;
    QStringList idefval;
    //    stepval.append("");
    //    typeval.append("芯片孔位");
    //    value_1val.append("");
    //    colorval.append("#5BA55A");
    //    pathval.append("qrc:/picture/pz_xpkw.png");
    stepval.append("");
    typeval.append("开始");
    value_1val.append("");
    colorval.append("#BCA718");
    pathval.append("qrc:/picture/pz_ks.png");
    while (query.next()) {
        stepval.append(query.value("step").toString());
        if(query.value("type").toString()=="1"){
            typeval.append("注射器");
            colorval.append("#00417E");
            pathval.append("qrc:/picture/pz_zsq.png");
        }else if(query.value("type").toString()=="2"){
            typeval.append("阀门");
            colorval.append("#5BA58C");
            pathval.append("qrc:/picture/pz_fm.png");
        }else if(query.value("type").toString()=="3"){
            typeval.append("转盘");
            colorval.append("#735BA5");
            pathval.append("qrc:/picture/pz_zp.png");
        }
        else if(query.value("type").toString()=="4")
        {
            typeval.append("盖盖");
            colorval.append("#9B7E47");
            pathval.append("qrc:/picture/gg.png");
        }
        else if(query.value("type").toString()=="5"){
            typeval.append("检测器");
            colorval.append("#5B66A4");
            pathval.append("qrc:/picture/pz_jc.png");
        }
        else if(query.value("type").toString()=="6"){
            typeval.append("转液");
            colorval.append("#3F9B44");
            pathval.append("qrc:/picture/pz_rdb2.png");
        }
        else if(query.value("type").toString()=="7"){
            typeval.append("等待");
            colorval.append("#FFB1D8");
            pathval.append("qrc:/picture/pz_dd.png");
        }
        else if(query.value("type").toString()=="8"){
            typeval.append("循环");
            colorval.append("#F0ABFF");
            pathval.append("qrc:/picture/pz_xh.png");
        }
        else if(query.value("type").toString()=="9")
        {
            typeval.append("跳过");
            colorval.append("#F3B46F");
            pathval.append("qrc:/picture/pz_tg.png");
        }
        else if(query.value("type").toString()=="10"){
            typeval.append("暂停");
            colorval.append("#89B5FF");
            pathval.append("qrc:/picture/pz_zt.png");
        }
        else if(query.value("type").toString()=="11"){
            typeval.append("加热");
            colorval.append("#ed1212");
            pathval.append("qrc:/picture/pz_jr.png");
        }
        value_1val.append(query.value("value_1").toString());
    }
    stepval.append("");
    typeval.append("结束");
    value_1val.append("");
    colorval.append("#A55B80");
    pathval.append("qrc:/picture/pz_js.png");
    QJsonArray buttonDataArray;
    QJsonObject rec;
    if(typeval.length()!=pathval.length()||typeval.length()!=colorval.length()
            ||typeval.length()!=value_1val.length()||typeval.length()!=stepval.length()){
        __LOG_ERROR("LengthError");
        return buttonDataArray;
    }
    for(int i = 0; i < stepval.size(); i++) {
        rec.insert("title", typeval[i]);
        rec.insert("icon", pathval[i]);
        rec.insert("color", colorval[i]);
        rec.insert("textval", value_1val[i]);
        rec.insert("numval", stepval[i]);
        buttonDataArray.append(rec);
    }
    return buttonDataArray;
}
/**
 * @brief 实验流程列表
 * @param 实验id
 */
QStringList ExeConfig::getlclist(QString id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type From exe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList resultdata;
    while (query.next()) {
        if(query.value("type").toString()=="1"){
            resultdata.append(query.value("step").toString()+"-注射器");
        }else if(query.value("type").toString()=="2"){
             resultdata.append(query.value("step").toString()+"-阀门");
        }else if(query.value("type").toString()=="3"){
             resultdata.append(query.value("step").toString()+"-转盘");
        }else if(query.value("type").toString()=="4"){
            resultdata.append(query.value("step").toString()+"-盖盖");
        }else if(query.value("type").toString()=="5"){
            resultdata.append(query.value("step").toString()+"-检测器");
        }else if(query.value("type").toString()=="6"){
            resultdata.append(query.value("step").toString()+"-转液");
        }else if(query.value("type").toString()=="7"){
            resultdata.append(query.value("step").toString()+"-等待");
        }else if(query.value("type").toString()=="8"){
            resultdata.append(query.value("step").toString()+"-循环");
        }else if(query.value("type").toString()=="9"){
            resultdata.append(query.value("step").toString()+"-跳过");
        }else if(query.value("type").toString()=="10"){
            resultdata.append(query.value("step").toString()+"-暂停");
        }else if(query.value("type").toString()=="11"){
            resultdata.append(query.value("step").toString()+"-加热");
        }
    }
    return resultdata;
}
/**
 * @brief 获取默认信息
 * @param 实验id
 * @return 是否默认 1是 0否
 */
int ExeConfig::getdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select isdef From exe_config Where id='%1' and  step=0  ").arg(id);
    query.exec(sql);
    while (query.next()) {
        int a = query.value("isdef").toInt();
        return a;
    }
    return 0;
}
/**
 * @brief 设置默认流程
 * @param 实验id
 */
void ExeConfig::setdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Update exe_config set isdef = 0 ";
    query.exec(sql);
    sql = QString("Update exe_config set isdef = 1 where id='%1' ").arg(id);
    query.exec(sql);
    emit updef();
    return;
}
/**
 * @brief 前端修改实验信息
 * @param 实验id
 * @param 实验名称
 * @param 耗材类型
 * @param 耗材图片路径
 */
void ExeConfig::wsyxx(QString id,QString name,QString type,QString path,QString start){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString newtype="";
    newtype=type;
    type=common->getItemCode("3",type);
    QString oldname="";
    QString oldtype="";
    QString sql = QString("select value_1,value_2 from exe_config where id='%1' and type=0 ").arg(id);
    query.exec(sql);
    while (query.next()){
        oldname.append(query.value("value_1").toString());
        oldtype.append(query.value("value_2").toString());
    }
    oldtype=common->getItemName("3",oldtype);
    if(true){
        QString sql = QString("select value_1 from exe_config where id!='%4' and type=0 ").arg(id);
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==name){
                emit rename();
                return;
            }
        }
    }
    sql=QString("Update exe_config set value_1='%1',value_2='%2',value_3='%3',value_4='%4' Where id='%5' and type=0  ").arg(name,type,path,start,id);
    query.exec(sql);
    sql=QString("Update zbexe_config set value_1='纯化',value_2='%1',value_3='%2' Where type=9 and value_2='%3' and value_3='%4' ").arg(newtype,name,oldtype,oldname);
    query.exec(sql);
    sql=QString("Update MainExperimentalProcess set PurifyingExp='%1' Where PurifyingExp='%2' ").arg(name,oldname);
    query.exec(sql);
    emit updatatab(id.toInt(),name,type,path);
    return;
}
/**
 * @brief 前端读实验名称
 * @param 实验id
 * @return 实验名称
 */
QStringList ExeConfig::rsyxx(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3,value_4 From exe_config Where id='%1' and  step=0 ").arg(id);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        res.append(query.value("value_1").toString());
        res.append(query.value("value_2").toString());
        res.append(query.value("value_3").toString());
        res.append(query.value("value_4").toString());
    }
    if(res.isEmpty()){
        __LOG_ERROR("ResLengthError");
        return {};
    }
    return {res[0],res[3]};
}
/**
 * @brief 前端读实验名称
 * @param 实验id
 * @return 实验名称
 */
QString ExeConfig::rsyty(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3 From exe_config Where id='%1' and  step=0 ").arg(id);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        res.append(query.value("value_1").toString());
        res.append(query.value("value_2").toString());
        res.append(query.value("value_3").toString());
    }
    if(res.length()==0){
        __LOG_ERROR("ResLengthError");
        return "";
    }
    res[1]=common->getItemName("3",res[1]);
    return res[1];
}
/**
 * @brief 前端读取实验信息列表
 * @param 实验名称
 * @return 实验信息列表
 */
QJsonArray ExeConfig::readsypzlist(QString name){
    if(name==""){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From exe_config Where step=0 order by id";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        if(syid.length()==0){
            return buttonDataArray;
        }
        if(syid.length()!=namelist.length()||syid.length()!=tyepelist.length()){
            return buttonDataArray;
        }
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }else{
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From exe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        for(int i=syid.size()-1;i>=0;i--){
            if(!namelist[i].contains(name)){
                syid.removeAt(i);
                namelist.removeAt(i);
                tyepelist.removeAt(i);
            }
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        if(syid.length()==0){
            return buttonDataArray;
        }
        if(syid.length()!=namelist.length()||syid.length()!=tyepelist.length()){
            return buttonDataArray;
        }
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }

}
/**
 * @brief 前端新建实验信息
 */
void ExeConfig::newsypz(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select id From exe_config Where step=0 ";
    query.exec(sql);
    QList<int> syid;
    while (query.next()) {
        syid.append(query.value("id").toInt());
    }
    int newid=1;
    for(int i=1;i<100;i++){
        if(!syid.contains(i)){
            newid=i;
            break;
        }
    }
    //    int randomNumber = QRandomGenerator::global()->bounded(100);
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 定义日期时间格式
    QString format = "yyyyMMddhhmmss";
    // 将当前日期时间按照指定格式转换为字符串
    QString currentDateTimeString = currentDateTime.toString(format);
    QString newname = currentDateTimeString;
    if(true){
        QString sql = "select value_1 from exe_config where type=0 ";
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==newname){
                return;
            }
        }
    }
    sql="Select value From Deftab Where id=3 and deffl=1 ";
    query.exec(sql);
    QString deftype;
    while (query.next()) {
        deftype=query.value("value").toString();
    }
    deftype=common->getItemCode("3",deftype);
    sql=QString("INSERT INTO exe_config ( id, isdef, step, type, value_1, value_2, value_3 ) "
                " VALUES ( '%1', '%2', '%3', '%4', '%5', '%6', '%7' );").arg(QString::number(newid),"0","0","0",newname,deftype,"纯化","qrc:/picture/pz_xp1.png");
    query.exec(sql);
    shunxunumb++;
    emit addlist();
    return;
}
/**
 * @brief 前端复制实验信息
 * @param 实验id
 */
void ExeConfig::copyypz(QString copyid){
    if(copyid!="0"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id From exe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        while (query.next()) {
            syid.append(query.value("id").toInt());
        }
        int newid=1;
        for(int i=1;i<100;i++){
            if(!syid.contains(i)){
                newid=i;
                break;
            }
        }
        sql=QString("Insert into exe_config select '%1',0,step,type,value_1,"
                    "value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 from exe_config where id = '%2' ").arg(QString::number(newid),copyid);
        query.exec(sql);
        // 获取当前日期和时间
        QDateTime currentDateTime = QDateTime::currentDateTime();
        // 定义日期时间格式
        QString format = "yyyyMMddhhmmss";
        // 将当前日期时间按照指定格式转换为字符串
        QString currentDateTimeString = currentDateTime.toString(format);
        QString newname = currentDateTimeString;
        sql=QString("Update exe_config set value_1='%1' Where id='%2' and type=0").arg(newname,QString::number(newid));
        query.exec(sql);
        emit addlist();
        return;
    }
}
/**
 * @brief 前端删除实验信息
 * @param 实验id
 */
void ExeConfig::delsypz(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("select value_1 from exe_config where id='%1' and type=0 ").arg(id);
    query.exec(sql);
    QString syname="";
    while (query.next()){
        syname=query.value("value_1").toString();
    }
    QStringList synamelist;
    sql = QString("select value_3 from zbexe_config where  type=9 ").arg(id);
    query.exec(sql);
    while (query.next()){
        synamelist.append(query.value("value_3").toString());
    }
    QStringList sylcnamelist;
    sql = "select PurifyingExp from MainExperimentalProcess  ";
    query.exec(sql);
    while (query.next()){
        sylcnamelist.append(query.value("PurifyingExp").toString());
    }
    if(synamelist.contains(syname)){
        emit delfail();
        return;
    }if(sylcnamelist.contains(syname)){
        emit delfaillc();
        return;
    }else{
        sql=QString("delete From exe_config Where id = '%1' ").arg(QString::number(id));
        query.exec(sql);
        sql = QString("UPDATE exe_config SET id = id - 1 WHERE id >'%1' ").arg(id);
        query.exec(sql);
        emit dellist();
        return;
    }
}
/**
 * @brief 前端更改信息
 * @param 实验id
 * @param 步骤id
 * @param 参数1-9
 */
void ExeConfig::upall(QString id,QString numv,QString v1,QString v2,QString v3,QString v4,QString v5,QString v6,QString v7,QString v8,QString v9){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update exe_config set value_1='%1',value_2='%2',value_3='%3',value_4='%4',value_5='%5',"
                        "value_6='%6',value_7='%7',value_8='%8',value_9='%9' Where id='%10' and step='%11' "
                        "").arg(v1,v2,v3,v4,v5,v6,v7,v8,v9,id,numv);
    query.exec(sql);
    sql = QString("Select type,value_1,value_2 From exe_config Where id='%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    QString kjlx,value2,value1;
    while (query.next()){
        kjlx=query.value("type").toString();
        value1=query.value("value_1").toString();
        value2=query.value("value_2").toString();
    }
    if(kjlx=="1"){
        sql=QString("Update exe_config set value_2='%1' Where id='%2' and type='%3' and value_1='%4' "
                    "").arg(value2,id,"1",value1);
        query.exec(sql);
    }
//    if(v1.contains("盖盖")){
//        sql = QString("Select value_1 From exe_config Where id='%1' and type='3' and step<'%2' ").arg(id,numv);
//        query.exec(sql);
//        QStringList jyv1;
//        while (query.next()){
//            jyv1.append(query.value("value_1").toString());
//        }
//        QString jyv1name = v1.replace(0,2,"接液");
//        if(!jyv1.contains(jyv1name)){
//            emit zpjy();
//            return;
//        }
//    }

    emit uplist();
    emit save();
    return;
}
/**
 * @brief 前端读控件信息
 * @param 实验id
 * @param 步骤id
 * @return 步骤参数
 */
QStringList ExeConfig::readall(QString id,QString numv){
    QStringList alldata;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 From exe_config Where id='%1' and  step='%2' ").arg(id,numv);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        alldata.append(query.value("value_1").toString());
        alldata.append(query.value("value_2").toString());
        alldata.append(query.value("value_3").toString());
        alldata.append(query.value("value_4").toString());
        alldata.append(query.value("value_5").toString());
        alldata.append(query.value("value_6").toString());
        alldata.append(query.value("value_7").toString());
        alldata.append(query.value("value_8").toString());
        alldata.append(query.value("value_9").toString());
    }
    if(alldata.length()==9){
        if(id=="1"&&alldata[8].isEmpty()){
            alldata[8]="mL/Min";
        }
        if(numv=="0"){
            alldata[1]=common->getItemName("3",alldata[1]);
        }
    }else{
        __LOG_ERROR("AllDataLengthError");
    }
    return alldata;
}
/**
 * @brief 删除某控件
 * @param 实验id
 * @param 步骤id
 */
void ExeConfig::delkj(QString id,QString numv){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    sql=QString("select value_3 From exe_config Where id = '%1' and type='7' and value_1='1' ").arg(id);
    query.exec(sql);
    QStringList syv3;
    while (query.next()){
        syv3.append(query.value("value_3").toString());
    }
    for(int i=0;i<syv3.length();i++){
        if(syv3[i]==numv){
            emit delv3();
            return;
        }
    }
    sql = QString("UPDATE exe_config SET value_3 = value_3 - 1 WHERE id = '%1' and value_3>'%2' and type='7' and value_1='1' ").arg(id,numv);
    query.exec(sql);
    sql=QString("delete From exe_config Where id = '%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE exe_config SET step = step - 1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    emit uplist();
}
/**
 * @brief 添加某控件
 * @param 实验id
 * @param 步骤id
 * @param 控件类型
 */
void ExeConfig::addkj(QString id,QString numv,QString type){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    sql = QString("UPDATE exe_config SET value_3 = value_3 +1 WHERE id = '%1' and value_3>'%2' and type='7' and value_1='1' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE exe_config SET step = step +1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    if(type=="8"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','是',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="2"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','开启',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="3"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','接液位置1',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="6"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','打液',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="10"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','暂停',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="4"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','是',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="5"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','大于',"
                    "'','','自动','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="7"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','0',"
                    "'5','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="1"){
        sql=QString("Select value_2 From exe_config Where id='%1' and  step='%2' ").arg(id,"0");
        QString sytype;
        query.exec(sql);
        while (query.next()){
            sytype=query.value("value_2").toString();
        }
        QString lx=getSyringTypezsq(sytype,"1");
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','1',"
                    "'','%2','打液','','','','','mL/Min' from exe_config where id = '%3' and step='%4'  ").arg(type,lx,id,numv);
    }else{
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','1',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }

    query.exec(sql);
    emit uplist();
}
/**
 * @brief 控件移动
 * @param 实验id
 * @param 位置部署
 * @param 开始位置
 */
void ExeConfig::kjchange(QString id,double mbwz,QString kswz){
    int sjwz = mbwz-66;
    int sx = sjwz%33;
    int sjnum;
    if(sx>16.5){
        sjnum = sjwz/33+1;
    }else{
        sjnum = sjwz/33;
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList alldata;
    if(kswz.toInt()<sjnum){
        QString sql = QString("SELECT id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 FROM exe_config where id='%1' and step='%2' ; ").arg(id,kswz);
        query.exec(sql);
        while (query.next()){
            alldata.append(query.value("id").toString());
            alldata.append(query.value("isdef").toString());
            alldata.append(query.value("step").toString());
            alldata.append(query.value("type").toString());
            alldata.append(query.value("value_1").toString());
            alldata.append(query.value("value_2").toString());
            alldata.append(query.value("value_3").toString());
            alldata.append(query.value("value_4").toString());
            alldata.append(query.value("value_5").toString());
            alldata.append(query.value("value_6").toString());
            alldata.append(query.value("value_7").toString());
            alldata.append(query.value("value_8").toString());
            alldata.append(query.value("value_9").toString());
        }
        if(alldata.length()!=13){
            __LOG_ERROR("AllDataLengthError");
            return;
        }
        sql = QString("DELETE  FROM exe_config where id='%1' and step='%2' ; ").arg(id,kswz);
        query.exec(sql);
        sql = QString("UPDATE exe_config SET step = step -1 WHERE id = '%1' and step>'%2' and step<='%3'").arg(id,kswz,QString::number(sjnum));
        query.exec(sql);
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,alldata[1],QString::number(sjnum),alldata[3]
                ,alldata[4],alldata[5],alldata[6],alldata[7],alldata[8],alldata[9],alldata[10],alldata[11],alldata[12]);
        query.exec(sql);
    }else if(kswz.toInt()>sjnum){
        sjnum+=1;
        QString sql = QString("SELECT id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 FROM exe_config where id='%1' and step='%2' ; ").arg(id,kswz);
        query.exec(sql);
        while (query.next()){
            alldata.append(query.value("id").toString());
            alldata.append(query.value("isdef").toString());
            alldata.append(query.value("step").toString());
            alldata.append(query.value("type").toString());
            alldata.append(query.value("value_1").toString());
            alldata.append(query.value("value_2").toString());
            alldata.append(query.value("value_3").toString());
            alldata.append(query.value("value_4").toString());
            alldata.append(query.value("value_5").toString());
            alldata.append(query.value("value_6").toString());
            alldata.append(query.value("value_7").toString());
            alldata.append(query.value("value_8").toString());
            alldata.append(query.value("value_9").toString());
        }
        if(alldata.length()!=13){
            __LOG_ERROR("AllDataLengthError");
            return;
        }
        sql = QString("DELETE  FROM exe_config where id='%1' and step='%2' ; ").arg(id,kswz);
        query.exec(sql);
        sql = QString("UPDATE exe_config SET step = step +1 WHERE id = '%1' and step<'%2' and step>='%3'").arg(id,kswz,QString::number(sjnum));
        query.exec(sql);
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12');").arg(id,alldata[1],QString::number(sjnum),alldata[3]
                ,alldata[4],alldata[5],alldata[6],alldata[7],alldata[8],alldata[9],alldata[10],alldata[11],alldata[12]);
        query.exec(sql);
    }
}
/**
 * @brief 控件移动添加
 * @param 实验id
 * @param 位置部署
 * @param 控件类型
 */
void ExeConfig::addkjchange(QString id,double mbwz,QString type){
    int sjwz = mbwz-32;
    int sx = sjwz%33;
    int sjnum;
    if(sx>16.5){
        sjnum = sjwz/33+1;
    }else{
        sjnum = sjwz/33;
    }
    if(sjnum<=0){
        return;
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    int maxStep;
    QString sql=QString("Select max(step) as Step From exe_config Where id='%1'").arg(id);
    query.exec(sql);
    while (query.next()) {
        maxStep=query.value("step").toInt();
    }
    if(maxStep<sjnum){
        sjnum=maxStep+1;
    }
    sql = QString("UPDATE exe_config SET step = step +1 WHERE id = '%1' and value_3>='%2' and type='7' and value_1='1' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql = QString("UPDATE exe_config SET step = step +1 WHERE id = '%1' and step>='%2' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql=QString("Select isdef From exe_config Where id='%1' and  step='0' ").arg(id);
    query.exec(sql);
    QString defv;
    while (query.next()){
        defv = query.value("isdef").toString();
    }
    if(type=="8"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"是","","","","","","","","");
    }else if(type=="2"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"开启","","","","","","","","");
    }else if(type=="6"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"打液","","","","","","","","");
    }else if(type=="10"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"暂停","","","","","","","","");
    }else if(type=="4"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"是","","","","","","","","");
    }else if(type=="5"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"大于","","","自动","","","","","");
    }else if(type=="7"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"0","5","","","","","","","");
    }else if(type=="1"){
        sql=QString("Select value_2 From exe_config Where id='%1' and  step='%2' ").arg(id,"0");
        QString sytype;
        query.exec(sql);
        while (query.next()){
            sytype=query.value("value_2").toString();
        }
        QString lx=getSyringTypezsq(sytype,"1");
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"1","",lx,"打液","","","","","mL/Min");
    }else if(type=="3"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"接液位置1","","","","","","","","");
    }else{
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"1","","","","","","","","");
    }

    query.exec(sql);
    emit uplist();
}

/*
 * 默认配置
 */
/**
 * @brief 读默认信息
 * @param 类型
 * @return 默认信息列表
 */
QJsonArray ExeConfig::getdeff(int val){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select deffl,value,num,value_2 From Deftab Where id='%1' order by num").arg(QString::number(val));
    query.exec(sql);
    QStringList isdef;
    QStringList namelist;
    QStringList numlist;
    QStringList namelist2;
    while (query.next()) {
        isdef.append(query.value("deffl").toString());
        namelist.append(query.value("value").toString());
        numlist.append(query.value("num").toString());
        namelist2.append(query.value("value_2").toString());
    }
    QJsonArray buttonDataArray;
    QJsonObject rec;
    if(isdef.length()!=namelist.length()&&isdef.length()!=namelist.length()
            &&isdef.length()!=numlist.length()&&isdef.length()!=namelist2.length()){
        __LOG_ERROR("LengthError");
        return buttonDataArray;
    }
    for(int i = 0; i < isdef.size(); ++i) {
        rec.insert("isdef", isdef[i]);
        rec.insert("name", namelist[i]);
        rec.insert("num", numlist[i]);
        rec.insert("namev", namelist2[i]);
        buttonDataArray.append(rec);
    }
    return buttonDataArray;
}
/**
 * @brief 默认信息修改
 * @param 类型
 * @param 编号
 * @param 参数1
 * @param 参数2
 */
void ExeConfig::updatadeff(int id,int num,QString name,QString namev){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    if(true){
        QString sql = QString("select value from Deftab where num!='%1' and id='%2' ").arg(QString::number(num),QString::number(id));
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==name){
                emit defrename();
                return;
            }
        }
    }
    QString sql=QString("Update Deftab set value='%1',value_2='%2' Where id='%3' and num='%4' "
                        "").arg(name,namev,QString::number(id),QString::number(num));
    query.exec(sql);

    if(id==3){
        sql=QString("Update SyringToalInfo Set Num='%1' Where ConsumablesTypes='%2'").arg(namev,QString::number(num));
        query.exec(sql);
    }
    return;
}
/**
 * @brief 默认信息添加
 * @param 类型
 */
void ExeConfig::adddeff(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select num From Deftab Where id='%1' ").arg(QString::number(id));
    query.exec(sql);
    QList<int> numlist;
    while (query.next()) {
        numlist.append(query.value("num").toInt());
    }
    int newid=1;
    for(int i=1;i<100;i++){
        if(!numlist.contains(i)){
            newid=i;
            break;
        }
    }
    sql=QString("INSERT INTO Deftab ( id, deffl, num, value ,value_2) "
                " VALUES ( '%1', '%2', '%3', '%4', '%5' );").arg(QString::number(id),"0",QString::number(newid),"","6");
    query.exec(sql);
    return;
}
/**
 * @brief 默认信息删除
 * @param 类型
 * @param 编号
 * @return 是否是默认信息
 */
bool ExeConfig::deldeff(int id,int num){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    if(id==3){
        QStringList hctype2;
        QString sql = "select ConsumablesTypes from SyringToalInfo ";
        query.exec(sql);
        while (query.next())
        {
            hctype2.append(query.value("ConsumablesTypes").toString());
        }
        if(hctype2.contains(QString::number(num))){
            return false;
        }
    }
    QString sql=QString("delete From Deftab Where id = '%1' and num = '%2' ").arg(QString::number(id),QString::number(num));
    query.exec(sql);
    sql = QString("UPDATE Deftab SET num = num - 1 WHERE id = '%1' and num>'%2' ").arg(QString::number(id),QString::number(num));
    query.exec(sql);

    if(id==3){
        sql=QString("Delete From SyringeMaintenance Where ConsumablesTypes='%1'").arg(QString::number(num));
        query.exec(sql);

        sql=QString("Delete From SyringToalInfo Where ConsumablesTypes='%1'").arg(QString::number(num));
        query.exec(sql);

        sql=QString("Update SyringeMaintenance Set ConsumablesTypes=ConsumablesTypes-1 Where ConsumablesTypes>'%1'").arg(QString::number(num));
        query.exec(sql);

        sql=QString("Update SyringToalInfo Set ConsumablesTypes=ConsumablesTypes-1 Where ConsumablesTypes>'%1'").arg(QString::number(num));
        query.exec(sql);
    }
    return true;
}
/**
 * @brief 空信息删除
 */
void ExeConfig::delnulldeff(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="delete From Deftab Where value = '' ";
    query.exec(sql);
    sql="delete from Deftab where id = '1' and not (substr(value, 1, 3) GLOB '[0-9]ml' or substr(value, 1, 4) GLOB '[0-9][0-9]ml' ); ";
    query.exec(sql);
}
/**
 * @brief 默认信息设置
 * @param 类型
 * @param 编号
 */
void ExeConfig::defset(int id,int num){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update Deftab set deffl = 0 where id='%1' ").arg(QString::number(id));
    query.exec(sql);
    sql = QString("Update Deftab set deffl = 1 where id='%1' and num = '%2' ").arg(QString::number(id),QString::number(num));
    query.exec(sql);
    return;
}
/**
 * @brief 注射器默认信息读取
 * @return 注射器列表
 */
QStringList ExeConfig::getzsqdef(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='1' and deffl='1' ";
    query.exec(sql);
    QStringList namelist;
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='1' and deffl='0' ";
    query.exec(sql);
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    return namelist;
}
/**
 * @brief 盖盖默认信息读取
 * @return 盖盖信息列表
 */
QStringList ExeConfig::getggdef(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='2' and deffl='1' ";
    query.exec(sql);
    QStringList namelist;
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='2' and deffl='0' ";
    query.exec(sql);
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    return namelist;
}
/**
 * @brief 耗材类型信息读取
 * @return 耗材类型信息列表
 */
QStringList ExeConfig::gethclxdef(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='3' and deffl='1' ";
    query.exec(sql);
    QStringList namelist;
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='3' and deffl='0' ";
    query.exec(sql);
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    return namelist;
}
/**
 * @brief 转盘耗材类型信息读取
 * @return 耗材类型添加空的信息列表
 */
QStringList ExeConfig::zpgethclxdef(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='3' and deffl='1' ";
    query.exec(sql);
    QStringList namelist;
    namelist.append("");
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='3' and deffl='0' ";
    query.exec(sql);
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    return namelist;
}
/**
 * @brief 读取密码
 * @return 密码
 */
QString ExeConfig::passwordRead(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='0'  ";
    query.exec(sql);
    QString password;
    while (query.next()) {
        password=query.value("value").toString();
    }
    return password;
}
/**
 * @brief 修改密码
 * @param 新密码
 */
void ExeConfig::passwordWrite(QString password){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update Deftab set value='%1' where id='0' "
                        "").arg(password);
    query.exec(sql);
    return;
}
Q_INVOKABLE int ExeConfig::checkPassword(QString password){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab where id='0'";
    query.exec(sql);
    QString currentPs;
    while(query.next()){
        currentPs=query.value("value").toString();
    }
    return password==currentPs?1:0;
}


Q_INVOKABLE QString ExeConfig::getSyringType(QString ConsumablesTypes,QString serial)
{
    QString code= common->getItemCode("3",ConsumablesTypes);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Type From SyringeMaintenance Where ConsumablesTypes='%1' and id='%2'").arg(code,serial);
    query.exec(sql);
    QString result;
    while (query.next())
    {
        result=query.value("Type").toString();
    }
    return result;
}


Q_INVOKABLE QString ExeConfig::getSyringTypezsq(QString ConsumablesTypes,QString serial)
{
    QString code= ConsumablesTypes;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Type From SyringeMaintenance Where ConsumablesTypes='%1' and id='%2'").arg(code,serial);
    query.exec(sql);
    QString result;
    while (query.next())
    {
        result=query.value("Type").toString();
    }
    return result;
}

Q_INVOKABLE int ExeConfig::getSyringeNumByConsumableName(QString consumableName){

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    int res = 0;
    QString sql=QString("Select value_2 from Deftab where value='%1'").arg(consumableName);
    query.exec(sql);
    while(query.next()){
        res = query.value("value_2").toInt();
    }

    return res;
}

Q_INVOKABLE QString ExeConfig::getDistance(QString ConsumablesTypes,QString serial,QString volume,QString deviation)
{
    QString code=common->getItemCode("3",ConsumablesTypes);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select InsideDiameter,PublicErrand From SyringeMaintenance Where ConsumablesTypes='%1' and id='%2'").arg(code,serial);
    query.exec(sql);
    double InsideDiameter;
    double PublicErrand;
    while (query.next()) {
        InsideDiameter=query.value("InsideDiameter").toDouble();
        PublicErrand=query.value("PublicErrand").toString().isNull()?0:query.value("PublicErrand").toDouble();
    }

    double r=(InsideDiameter+PublicErrand)/2;
    double area=3.1416*r*r;
    double result=(volume.toDouble()/area)*1000+deviation.toDouble();

    return QString::number(result,'f',4);
}

Q_INVOKABLE QString ExeConfig::getVolume(QString ConsumablesTypes,QString serial,QString distance,QString deviation){
    QString code= common->getItemCode("3",ConsumablesTypes);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select InsideDiameter,PublicErrand From SyringeMaintenance Where ConsumablesTypes='%1' and id='%2'").arg(code,serial);
    query.exec(sql);
    double InsideDiameter;
    double PublicErrand;
    while (query.next()) {
        InsideDiameter=query.value("InsideDiameter").toDouble();
        PublicErrand=query.value("PublicErrand").toString().isNull()?0:query.value("PublicErrand").toDouble();
    }
    double r=(InsideDiameter+PublicErrand)/2;
    double area=3.1416*r*r;
    double result=(distance.toDouble()+deviation.toDouble())*area/1000;

    return result==0?"":QString::number(result,'f',4);
}

Q_INVOKABLE void ExeConfig::resetAllSyringe(QString ConsumablesTypes){
    udpClient=UdpClient::getInstance();
    int code=common->getItemCode("3",ConsumablesTypes).toInt();
    QString info=QString("62 00 00 3C 00 07 08 %1 E7 2F 55 AA").arg(common->TenToSixteen(code,4));
    udpClient->ClientSendingData(common->GetSendData(info));
}

//导出
Q_INVOKABLE int ExeConfig::exportExeConfig(int module,QString id)
{
    QString path="";
    QString  savePath = QFileDialog::getSaveFileName(NULL, "保存", path,"Wlk files(*.txt)");
    if(!savePath.contains(".txt")){     // 兼容Linux端获取到的路径不带后缀的问题
        savePath.append(".txt");
    }
    if(savePath.isEmpty()&&savePath.isNull()){
        return -1;
    }
    if(QFile::exists(savePath)){
        QFile::remove(savePath);
    }
    QFile file(savePath);
    file.open(QIODevice::Append);

    QTextStream out(&file);  // QTextStream会自动将 Unicode 编码同操作系统的编码进行转换
    QTextCodec* codec = QTextCodec::codecForName("GBK");
    out.setCodec(codec);
    out<<QString::number(module) + "\n";

    QString dbName;
    switch (module) {
    case 1:
        dbName="fbexe_config";
        break;
    case 2:
        dbName="dbexe_config";
        break;
    case 3:
        dbName="zbexe_config";
        break;
    case 4:
        dbName="exe_config";

    }

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select count(*) num From %1 Where Id='%2'").arg(dbName,id);
    query.exec(sql);
    int dataNum;
    while (query.next()) {
        dataNum=query.value("num").toInt();
    }

    if(dataNum>0){
        sql =QString("Select * From %1 Where Id='%2'").arg(dbName,id);

        query.exec(sql);
        while (query.next())
        {
            QString addSql=QString("Insert into %14 Values('%1','%2','%3','%4','%5','%6','%7','%8',"
                                   "'%9','%10','%11','%12','%13')").arg(
                        query.value("id").toString(),"0",query.value("step").toString(),
                        query.value("type").toString(),query.value("value_1").toString(),
                        query.value("value_2").toString(),query.value("value_3").toString(),
                        query.value("value_4").toString(),query.value("value_5").toString(),
                        query.value("value_6").toString(),query.value("value_7").toString(),
                        query.value("value_8").toString(),query.value("value_9").toString(),dbName);
            out<<addSql+"\n";
        }
        file.close();
    }
    else{
        return -3;
    }

    return 0;
}

//导入
Q_INVOKABLE int ExeConfig::importExeConfig(int module)
{
    QStringList sfilePath;
    QFileDialog *fDialog=new QFileDialog(NULL,"file dlg","","Txt files(*.txt)");
    if(fDialog->exec()==QFileDialog::Accepted)
    {
        sfilePath = fDialog->selectedFiles();
    }
    if(sfilePath.length()<=0)   return 1;
    QString filepath = sfilePath[0];
    QFile file(filepath);
    if(!file.exists()){
        return -1;
    }

    QTextCodec* codec = QTextCodec::codecForName("GBK");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream txtInput(&file);
    txtInput.setCodec(codec);

    QString serial;
    QString dbName;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 定义日期时间格式
    QString format = "yyyyMMddhhmmss";
    QString name= currentDateTime.toString(format);
    int i=0;
    while (!txtInput.atEnd())
    {
        QString lineStr = txtInput.readLine();
        if(i==0 ){
            if(lineStr.toInt()!=module)
            {
                return -2;
            }
            else{
                i++;
                switch (module) {
                case 1:
                    dbName="fbexe_config";
                    break;
                case 2:
                    dbName="dbexe_config";
                    break;
                case 3:
                    dbName="zbexe_config";
                    break;
                case 4:
                    dbName="exe_config";
                    break;
                }

                QString sql=QString("Select max(id)+1 as Serial From %1").arg(dbName);
                query.exec(sql);
                while (query.next()) {
                    serial=query.value("Serial").toString().isNull()?"1":query.value("Serial").toString();
                }
            }
        }
        else if(i!=0 && !lineStr.contains(QString("Insert into %1").arg(dbName))){
            return -2;
        }
        else{
            QStringList strList = lineStr.split("'");
            if(strList.size() < 10){ __LOG_ERROR("StrListSplitError "+strList.join(",")); return -1; }      // 校验
            strList[1]=serial;
            if(i==1){
                strList[9]=name;
                i++;
            }
            lineStr = strList.join("'");
            query.exec(lineStr);
        }
    }
    QString sql=QString("Select count(*) num From %1 Where Id='%2'").arg(dbName,serial);
    query.exec(sql);
    int dataNum;
    while (query.next()) {
        dataNum=query.value("num").toInt();
    }
    if(dataNum==0){
        return -2;
    }
    return 0;
}

Q_INVOKABLE int ExeConfig::exportMainExeConfig(QString serial){
    QString path="";
    QString  savePath = QFileDialog::getSaveFileName(NULL, "保存", path,"Wlk files(*.txt)");
    if(!savePath.contains(".txt")){     // 兼容Linux端获取到的路径不带后缀的问题
        savePath.append(".txt");
    }

    if(savePath.isEmpty()&&savePath.isNull()){
        return -1;
    }
    if(QFile::exists(savePath)){
        QFile::remove(savePath);
    }
    QFile file(savePath);
    file.open(QIODevice::Append);

    QTextStream out(&file);  // QTextStream会自动将 Unicode 编码同操作系统的编码进行转换
    QTextCodec* codec = QTextCodec::codecForName("GBK");
    out.setCodec(codec);
    out<<QString::number(0) + "\n";

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("Select * From MainExperimentalProcess Where serial='%1'").arg(serial);
    query.exec(sql);
    QString fbName,dbName,zbName,chName;
    while (query.next()) {
        fbName=query.value("ReleasseTargetExp").toString();
        dbName=query.value("PracticeTargetExp").toString();
        zbName=query.value("ShiftingTargetExp").toString();
        chName=query.value("PurifyingExp").toString();
    }
    QStringList list;
    list.append("fbexe_config,"+fbName);
    list.append("dbexe_config,"+dbName);
    list.append("zbexe_config,"+zbName);
    list.append("exe_config,"+chName);

    for(int i=0;i<list.length();i++){
        QStringList infoList= list[i].split(",");
        if(infoList.size() < 2){ __LOG_ERROR("StrListSplitError "+infoList.join(",")); return -1; }      // 校验
        sql=QString("Select id From %1 Where value_1='%2' and step=0").arg(infoList[0],infoList[1]);
        query.exec(sql);
        QString id;
        while (query.next()) {
            id=query.value("Id").toString();
        }
        sql =QString("Select * From %1 Where Id='%2'").arg(infoList[0],id);
        query.exec(sql);
        while (query.next())
        {
            QString addSql=QString("Insert into %14 Values('%1','%2','%3','%4','%5','%6','%7','%8',"
                                   "'%9','%10','%11','%12','%13')").arg(
                        query.value("id").toString(),"0",query.value("step").toString(),
                        query.value("type").toString(),query.value("value_1").toString(),
                        query.value("value_2").toString(),query.value("value_3").toString(),
                        query.value("value_4").toString(),query.value("value_5").toString(),
                        query.value("value_6").toString(),query.value("value_7").toString(),
                        query.value("value_8").toString(),query.value("value_9").toString(),infoList[0]);
            out<<addSql+"\n";
        }
    }
    file.close();
    return 0;
}

Q_INVOKABLE int ExeConfig::importMainExeConfig(){
    QStringList sfilePath;
    QFileDialog *fDialog=new QFileDialog(NULL,"file dlg","","Txt files(*.txt)");
    if(fDialog->exec()==QFileDialog::Accepted)
    {
        sfilePath = fDialog->selectedFiles();
    }
    if(sfilePath.length()<=0)   return 1;
    QString filepath = sfilePath[0];
    QFile file(filepath);
    if(!file.exists()){
        return -1;
    }
    QTextCodec* codec = QTextCodec::codecForName("GBK");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream txtInput(&file);
    txtInput.setCodec(codec);

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 定义日期时间格式
    QString format = "yyyyMMddhhmmss";
    QString name= currentDateTime.toString(format);
    int i=0;

    QString fbSerial,dbSerial,zbSerial,chSerial,mainSerial;

    QString sql=QString("Select max(id)+1 as Serial From %1").arg("fbexe_config");
    query.exec(sql);
    while (query.next()) {
        fbSerial=query.value("Serial").toString().isNull()?"1":query.value("Serial").toString();
    }
    sql=QString("Select max(id)+1 as Serial From %1").arg("dbexe_config");
    query.exec(sql);
    while (query.next()) {
        dbSerial=query.value("Serial").toString().isNull()?"1":query.value("Serial").toString();
    }
    sql=QString("Select max(id)+1 as Serial From %1").arg("zbexe_config");
    query.exec(sql);
    while (query.next()) {
        zbSerial=query.value("Serial").toString().isNull()?"1":query.value("Serial").toString();
    }
    sql=QString("Select max(id)+1 as Serial From %1").arg("exe_config");
    query.exec(sql);
    while (query.next()) {
        chSerial=query.value("Serial").toString().isNull()?"1":query.value("Serial").toString();
    }
    sql=QString("Select max(Serial)+1 as Serial From %1").arg("MainExperimentalProcess");
    query.exec(sql);
    while (query.next()) {
        mainSerial=query.value("Serial").toString().isNull()?"1":query.value("Serial").toString();
    }

    while (!txtInput.atEnd())
    {
        QString lineStr = txtInput.readLine();
        if(i==0 ){
            if(lineStr.toInt()!=0)
            {
                return -2;
            }
            i++;
        }
        else{
            QStringList strList = lineStr.split("'");
            if(strList.size() < 10){ __LOG_ERROR("StrListSplitError "+strList.join(",")); return -1; }      // 校验
            if(lineStr.contains("fbexe_config")){
                strList[1]=fbSerial;
            }
            else if(lineStr.contains("dbexe_config")){
                strList[1]=dbSerial;

            }
            else if(lineStr.contains("zbexe_config")){
                strList[1]=zbSerial;

            }
            else if(lineStr.contains("exe_config")){
                strList[1]=chSerial;

            }
            else{
                return -2;
            }
            if(strList[5]=="0"){
                strList[9]=name;
                i++;
            }
            lineStr = strList.join("'");
            query.exec(lineStr);
        }
    }

    sql=QString("Insert into MainExperimentalProcess values('%1','%2','%2','%2','%2','%2','0')").arg(mainSerial,name);
    query.exec(sql);
    return 0;
}

/*
 * 放靶实验流程配置
 */
/**
 * @brief 数据库读某一实验流程
 * @param 实验id
 * @return 实验流程列表
 */
QJsonArray ExeConfig::fbreadsypz(int id)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type,value_1 From fbexe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList stepval;
    QStringList typeval;
    QStringList value_1val;
    QStringList colorval;
    QStringList pathval;
    QStringList idefval;
    //    stepval.append("");
    //    typeval.append("实验信息");
    //    value_1val.append("");
    //    colorval.append("#5BA55A");
    //    pathval.append("qrc:/picture/pz_xpkw.png");
    stepval.append("");
    typeval.append("开始");
    value_1val.append("");
    colorval.append("#BCA718");
    pathval.append("qrc:/picture/pz_ks.png");
    while (query.next()) {
        stepval.append(query.value("step").toString());
        if(query.value("type").toString()=="1"){
            typeval.append("挡板");
            colorval.append("#00417E");
            pathval.append("qrc:/picture/pz_db2.png");
        }else if(query.value("type").toString()=="2"){
            typeval.append("取放靶");
            colorval.append("#5BA58C");
            pathval.append("qrc:/picture/pz_quba2.png");
        }else if(query.value("type").toString()=="3"){
            typeval.append("夹爪");
            colorval.append("#705BA5");
            pathval.append("qrc:/picture/pz_jz2.png");
        }else if(query.value("type").toString()=="4"){
            typeval.append("开盖");
            colorval.append("#9B7E47");
            pathval.append("qrc:/picture/pz_kg2.png");
        }else if(query.value("type").toString()=="5"){
            typeval.append("热室端送靶");
            colorval.append("#89439B");
            pathval.append("qrc:/picture/pz_cb2.png");
        }else if(query.value("type").toString()=="6"){
            typeval.append("转液");
            colorval.append("#3F9B44");
            pathval.append("qrc:/picture/pz_rdb2.png");
        }else if(query.value("type").toString()=="7"){
            typeval.append("等待");
            colorval.append("#FFB1D8");
            pathval.append("qrc:/picture/pz_dd.png");
        }else if(query.value("type").toString()=="8"){
            typeval.append("暂停");
            colorval.append("#89B5FF");
            pathval.append("qrc:/picture/pz_zt.png");
        }else if(query.value("type").toString()=="9"){
            typeval.append("联动");
            colorval.append("#7EE3D8");
            pathval.append("qrc:/picture/pz_ld.png");
        }
        value_1val.append(query.value("value_1").toString());
    }
    stepval.append("");
    typeval.append("结束");
    value_1val.append("");
    colorval.append("#A55B80");
    pathval.append("qrc:/picture/pz_js.png");
    QJsonArray buttonDataArray;
    QJsonObject rec;
    if(typeval.length()!=pathval.length()||typeval.length()!=colorval.length()
            ||typeval.length()!=value_1val.length()||typeval.length()!=stepval.length()){
        __LOG_ERROR("LengthError");
        return buttonDataArray;
    }
    for(int i = 0; i < stepval.size(); i++) {
        rec.insert("title", typeval[i]);
        rec.insert("icon", pathval[i]);
        rec.insert("color", colorval[i]);
        rec.insert("textval", value_1val[i]);
        rec.insert("numval", stepval[i]);
        buttonDataArray.append(rec);
    }
    return buttonDataArray;
}
/**
 * @brief 获取默认信息
 * @param 实验id
 * @return 是否默认 1是 0否
 */
int ExeConfig::fbgetdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select isdef From fbexe_config Where id='%1' and  step=0  ").arg(id);
    query.exec(sql);
    while (query.next()) {
        int a = query.value("isdef").toInt();
        return a;
    }
    return 0;
}
/**
 * @brief 设置默认流程
 * @param 实验id
 */
void ExeConfig::fbsetdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Update fbexe_config set isdef = 0 ";
    query.exec(sql);
    sql = QString("Update fbexe_config set isdef = 1 where id='%1' ").arg(id);
    query.exec(sql);
    emit updef();
    return;
}
/**
 * @brief 前端修改实验信息
 * @param 实验id
 * @param 实验名称
 * @param 耗材类型
 */
void ExeConfig::fbwsyxx(QString id,QString name,QString type){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    //    type=common->getItemCode("3",type);
    QString oldname="";
    QString sql = QString("select value_1 from fbexe_config where id='%1' and type=0 ").arg(id);
    query.exec(sql);
    while (query.next()){
        oldname.append(query.value("value_1").toString());
    }
    if(true){
        QString sql = QString("select value_1 from fbexe_config where id!='%4' and type=0 ").arg(id);
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==name){
                emit rename();
                return;
            }
        }
    }
    sql=QString("Update fbexe_config set value_1='%1',value_2='%2' Where id='%3' and type=0  ").arg(name,type,id);
    query.exec(sql);
    sql=QString("Update MainExperimentalProcess set ReleasseTargetExp='%1' Where ReleasseTargetExp='%2' ").arg(name,oldname);
    query.exec(sql);
    emit updatatab(id.toInt(),name,type,"");
    return;
}
/**
 * @brief 前端读实验名称
 * @param 实验id
 * @return 实验名称
 */
QStringList ExeConfig::fbrsyxx(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3 From fbexe_config Where id='%1' and  step=0 ").arg(id);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        res.append(query.value("value_1").toString());
        res.append(query.value("value_2").toString());
        res.append(query.value("value_3").toString());
    }
    if(res.isEmpty()){
        __LOG_ERROR("ResIsEmpty");
        return {};
    }
    return {res[0],res[1]};
}
/**
 * @brief 前端读取实验信息列表
 * @param 实验名称
 * @return 实验信息列表
 */
QJsonArray ExeConfig::fbreadsypzlist(QString name){
    if(name==""){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From fbexe_config Where step=0 order by id";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        if(syid.length()==0){
            __LOG_ERROR("Length=0");
            return buttonDataArray;
        }
        if(syid.length()!=namelist.length()||syid.length()!=tyepelist.length()){
            __LOG_ERROR("LengthError");
            return buttonDataArray;
        }
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }else{
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From fbexe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        for(int i=syid.size()-1;i>=0;i--){
            if(!namelist[i].contains(name)){
                syid.removeAt(i);
                namelist.removeAt(i);
                tyepelist.removeAt(i);
            }
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        if(syid.length()==0){
            __LOG_ERROR("Length=0");
            return buttonDataArray;
        }
        if(syid.length()!=namelist.length()||syid.length()!=tyepelist.length()){
            __LOG_ERROR("LengthError");
            return buttonDataArray;
        }
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }

}
/**
 * @brief 前端新建实验信息
 */
void ExeConfig::fbnewsypz(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select id From fbexe_config Where step=0 ";
    query.exec(sql);
    QList<int> syid;
    while (query.next()) {
        syid.append(query.value("id").toInt());
    }
    int newid=1;
    for(int i=1;i<100;i++){
        if(!syid.contains(i)){
            newid=i;
            break;
        }
    }
    //    int randomNumber = QRandomGenerator::global()->bounded(100);
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 定义日期时间格式
    QString format = "yyyyMMddhhmmss";
    // 将当前日期时间按照指定格式转换为字符串
    QString currentDateTimeString = currentDateTime.toString(format);
    QString newname = currentDateTimeString;
    if(true){
        QString sql = "select value_1 from fbexe_config where type=0 ";
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==newname){
                return;
            }
        }
    }
    sql="Select value From Deftab Where id=3 and deffl=1 ";
    query.exec(sql);
    QString deftype;
    while (query.next()) {
        deftype=query.value("value").toString();
    }
    deftype=common->getItemCode("3",deftype);
    sql=QString("INSERT INTO fbexe_config ( id, isdef, step, type, value_1, value_2 ) "
                " VALUES ( '%1', '%2', '%3', '%4', '%5', '%6' );").arg(QString::number(newid),"0","0","0",newname,"放靶");
    query.exec(sql);
    shunxunumb++;
    emit addlist();
    return;
}
/**
 * @brief 前端复制实验信息
 * @param 实验id
 */
void ExeConfig::fbcopyypz(QString copyid){
    if(copyid!="0"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id From fbexe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        while (query.next()) {
            syid.append(query.value("id").toInt());
        }
        int newid=1;
        for(int i=1;i<100;i++){
            if(!syid.contains(i)){
                newid=i;
                break;
            }
        }
        sql=QString("Insert into fbexe_config select '%1',0,step,type,value_1,"
                    "value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 from fbexe_config where id = '%2' ").arg(QString::number(newid),copyid);
        query.exec(sql);
        // 获取当前日期和时间
        QDateTime currentDateTime = QDateTime::currentDateTime();
        // 定义日期时间格式
        QString format = "yyyyMMddhhmmss";
        // 将当前日期时间按照指定格式转换为字符串
        QString currentDateTimeString = currentDateTime.toString(format);
        QString newname = currentDateTimeString;
        sql=QString("Update fbexe_config set value_1='%1' Where id='%2' and type=0").arg(newname,QString::number(newid));
        query.exec(sql);
        emit addlist();
        return;
    }
}
/**
 * @brief 前端删除实验信息
 * @param 实验id
 */
void ExeConfig::fbdelsypz(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("select value_1 from fbexe_config where id='%1' and type=0 ").arg(id);
    query.exec(sql);
    QString syname="";
    while (query.next()){
        syname=query.value("value_1").toString();
    }
    QStringList sylcnamelist;
    sql = "select ReleasseTargetExp from MainExperimentalProcess  ";
    query.exec(sql);
    while (query.next()){
        sylcnamelist.append(query.value("ReleasseTargetExp").toString());
    }
    if(sylcnamelist.contains(syname)){
        emit delfailfb();
        return;
    }else{
        sql=QString("delete From fbexe_config Where id = '%1' ").arg(QString::number(id));
        query.exec(sql);
        sql = QString("UPDATE fbexe_config SET id = id - 1 WHERE id >'%1' ").arg(id);
        query.exec(sql);
        emit dellist();
        return;
    }
}
/**
 * @brief 前端更改信息
 * @param 实验id
 * @param 步骤id
 * @param 参数1-9
 */
void ExeConfig::fbupall(QString id,QString numv,QString v1,QString v2,QString v3,QString v4,QString v5,QString v6,QString v7,QString v8,QString v9){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update fbexe_config set value_1='%1',value_2='%2',value_3='%3',value_4='%4',value_5='%5',"
                        "value_6='%6',value_7='%7',value_8='%8',value_9='%9' Where id='%10' and step='%11' "
                        "").arg(v1,v2,v3,v4,v5,v6,v7,v8,v9,id,numv);
    query.exec(sql);
    sql = QString("Select type,value_1,value_2 From fbexe_config Where id='%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    QString kjlx,value2,value1;
    while (query.next()){
        kjlx=query.value("type").toString();
        value1=query.value("value_1").toString();
        value2=query.value("value_2").toString();
    }
    //    if(kjlx=="1"){
    //        sql=QString("Update fbexe_config set value_2='%1' Where id='%2' and type='%3' and value_1='%4' "
    //                    "").arg(value2,id,"1",value1);
    //        query.exec(sql);
    //    }
    emit uplist();
    emit save();
    return;
}
/**
 * @brief 前端读控件信息
 * @param 实验id
 * @param 步骤id
 * @return 步骤参数
 */
QStringList ExeConfig::fbreadall(QString id,QString numv){
    QStringList alldata;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 From fbexe_config Where id='%1' and  step='%2' ").arg(id,numv);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        alldata.append(query.value("value_1").toString());
        alldata.append(query.value("value_2").toString());
        alldata.append(query.value("value_3").toString());
        alldata.append(query.value("value_4").toString());
        alldata.append(query.value("value_5").toString());
        alldata.append(query.value("value_6").toString());
        alldata.append(query.value("value_7").toString());
        alldata.append(query.value("value_8").toString());
        alldata.append(query.value("value_9").toString());
    }
    return alldata;
}
/**
 * @brief 删除某控件
 * @param 实验id
 * @param 步骤id
 */
void ExeConfig::fbdelkj(QString id,QString numv){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    sql=QString("select value_3 From fbexe_config Where id = '%1' and type='7' and value_1='1' ").arg(id);
    query.exec(sql);
    QStringList syv3;
    while (query.next()){
        syv3.append(query.value("value_3").toString());
    }
    for(int i=0;i<syv3.length();i++){
        if(syv3[i]==numv){
            emit delv3();
            return;
        }
    }
    sql = QString("UPDATE fbexe_config SET value_3 = value_3 - 1 WHERE id = '%1' and value_3>'%2' and type='7' and value_1='1' ").arg(id,numv);
    query.exec(sql);
    sql=QString("delete From fbexe_config Where id = '%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE fbexe_config SET step = step - 1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    emit uplist();
}
/**
 * @brief 添加某控件
 * @param 实验id
 * @param 步骤id
 * @param 控件类型
 */
void ExeConfig::fbaddkj(QString id,QString numv,QString type){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    sql = QString("UPDATE fbexe_config SET value_3 = value_3 +1 WHERE id = '%1' and value_3>'%2' and type='7' and value_1='1' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE fbexe_config SET step = step +1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    if(type=="1"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','遮挡',"
                    "'','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="2"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','取靶',"
                    "'','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="3"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','夹紧',"
                    "'','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="4"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','开盖',"
                    "'','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="5"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','热室端送靶',"
                    "'','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="6"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','吸液',"
                    "'','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="7"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','0',"
                    "'5','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="8"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','暂停',"
                    "'','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="9"){
        sql=QString("Insert into fbexe_config select id,isdef,step+1,'%1','纯化',"
                    "'','','','','','','','' from fbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }
    query.exec(sql);
    emit uplist();
}
/**
 * @brief 控件移动添加
 * @param 实验id
 * @param 位置部署
 * @param 控件类型
 */
void ExeConfig::fbaddkjchange(QString id,double mbwz,QString type){
    int sjwz = mbwz-32;
    int sx = sjwz%33;
    int sjnum;
    if(sx>16.5){
        sjnum = sjwz/33+1;
    }else{
        sjnum = sjwz/33;
    }
    if(sjnum<=0){
        return;
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    int maxStep;
    QString sql=QString("Select max(step) as Step From fbexe_config Where id='%1'").arg(id);
    query.exec(sql);
    while (query.next()) {
        maxStep=query.value("step").toInt();
    }
    if(maxStep<sjnum){
        sjnum=maxStep+1;
    }
    sql = QString("UPDATE fbexe_config SET step = step +1 WHERE id = '%1' and value_3>='%2' and type='7' and value_1='1' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql = QString("UPDATE fbexe_config SET step = step +1 WHERE id = '%1' and step>='%2' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql=QString("Select isdef From fbexe_config Where id='%1' and  step='0' ").arg(id);
    query.exec(sql);
    QString defv;
    while (query.next()){
        defv = query.value("isdef").toString();
    }
    if(type=="1"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"遮挡","","","","","","","","");
    }else if(type=="2"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"取靶","","","","","","","","");
    }else if(type=="3"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"夹紧","","","","","","","","");
    }else if(type=="4"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"开盖","","","","","","","","");
    }else if(type=="5"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"热室端送靶","","","","","","","","");
    }else if(type=="6"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"吸液","","","","","","","","");
    }else if(type=="7"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"0","5","","","","","","","");
    }else if(type=="8"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"暂停","","","","","","","","");
    }else if(type=="9"){
        sql=QString("INSERT INTO fbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"纯化","","","","","","","","");
    }

    query.exec(sql);
    emit uplist();
}
/**
 * @brief 实验流程列表
 * @param 实验id
 */
QStringList ExeConfig::fbgetlclist(QString id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type From fbexe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList resultdata;
    while (query.next()) {
        if(query.value("type").toString()=="1"){
            resultdata.append(query.value("step").toString()+"-挡板");
        }else if(query.value("type").toString()=="2"){
             resultdata.append(query.value("step").toString()+"-取放靶");
        }else if(query.value("type").toString()=="3"){
             resultdata.append(query.value("step").toString()+"-夹爪");
        }else if(query.value("type").toString()=="4"){
            resultdata.append(query.value("step").toString()+"-开盖");
        }else if(query.value("type").toString()=="5"){
            resultdata.append(query.value("step").toString()+"-热室端送靶");
        }else if(query.value("type").toString()=="6"){
            resultdata.append(query.value("step").toString()+"-转液");
        }else if(query.value("type").toString()=="7"){
            resultdata.append(query.value("step").toString()+"-等待");
        }else if(query.value("type").toString()=="8"){
            resultdata.append(query.value("step").toString()+"-暂停");
        }else if(query.value("type").toString()=="9"){
            resultdata.append(query.value("step").toString()+"-联动");
        }
    }
    return resultdata;
}

/*
 * 转靶实验流程配置
 */
/**
 * @brief 数据库读某一实验流程
 * @param 实验id
 * @return 实验流程列表
 */
QJsonArray ExeConfig::zbreadsypz(int id)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type,value_1 From zbexe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList stepval;
    QStringList typeval;
    QStringList value_1val;
    QStringList colorval;
    QStringList pathval;
    QStringList idefval;
    //    stepval.append("");
    //    typeval.append("实验信息");
    //    value_1val.append("");
    //    colorval.append("#5BA55A");
    //    pathval.append("qrc:/picture/pz_xpkw.png");
    stepval.append("");
    typeval.append("开始");
    value_1val.append("");
    colorval.append("#BCA718");
    pathval.append("qrc:/picture/pz_ks.png");
    while (query.next()) {
        stepval.append(query.value("step").toString());
        if(query.value("type").toString()=="1"){
            typeval.append("挡板");
            colorval.append("#00417E");
            pathval.append("qrc:/picture/pz_db2.png");
        }else if(query.value("type").toString()=="2"){
            typeval.append("取放靶");
            colorval.append("#5BA58C");
            pathval.append("qrc:/picture/pz_quba2.png");
        }else if(query.value("type").toString()=="3"){
            typeval.append("夹爪");
            colorval.append("#705BA5");
            pathval.append("qrc:/picture/pz_jz2.png");
        }else if(query.value("type").toString()=="4"){
            typeval.append("旋盖");
            colorval.append("#9B7E47");
            pathval.append("qrc:/picture/pz_kg2.png");
        }else if(query.value("type").toString()=="5"){
            typeval.append("热室端送靶");
            colorval.append("#89439B");
            pathval.append("qrc:/picture/pz_cb2.png");
        }else if(query.value("type").toString()=="6"){
            typeval.append("转液");
            colorval.append("#3F9B44");
            pathval.append("qrc:/picture/pz_rdb2.png");
        }else if(query.value("type").toString()=="10"){
            typeval.append("活性检测");
            colorval.append("#1240ed");
            pathval.append("qrc:/picture/pz_hx.png");
        }else if(query.value("type").toString()=="7"){
            typeval.append("等待");
            colorval.append("#FFB1D8");
            pathval.append("qrc:/picture/pz_dd.png");
        }else if(query.value("type").toString()=="8"){
            typeval.append("暂停");
            colorval.append("#89B5FF");
            pathval.append("qrc:/picture/pz_zt.png");
        }else if(query.value("type").toString()=="9"){
            typeval.append("联动");
            colorval.append("#7EE3D8");
            pathval.append("qrc:/picture/pz_ld.png");
        }else if(query.value("type").toString()=="11"){
            typeval.append("旋盖夹爪");
            colorval.append("#B16B19");
            pathval.append("qrc:/picture/pz_xgjz.png");
        }

        value_1val.append(query.value("value_1").toString());
    }
    stepval.append("");
    typeval.append("结束");
    value_1val.append("");
    colorval.append("#A55B80");
    pathval.append("qrc:/picture/pz_js.png");
    QJsonArray buttonDataArray;
    QJsonObject rec;
    if(typeval.length()!=pathval.length()||typeval.length()!=colorval.length()
            ||typeval.length()!=value_1val.length()||typeval.length()!=stepval.length()){
        __LOG_ERROR("LengthError");
        return buttonDataArray;
    }
    for(int i = 0; i < stepval.size(); i++) {
        rec.insert("title", typeval[i]);
        rec.insert("icon", pathval[i]);
        rec.insert("color", colorval[i]);
        rec.insert("textval", value_1val[i]);
        rec.insert("numval", stepval[i]);
        buttonDataArray.append(rec);
    }
    return buttonDataArray;
}
/**
 * @brief 实验流程列表
 * @param 实验id
 */
QStringList ExeConfig::zbgetlclist(QString id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type From zbexe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList resultdata;
    while (query.next()) {
        if(query.value("type").toString()=="1"){
            resultdata.append(query.value("step").toString()+"-挡板");
        }else if(query.value("type").toString()=="2"){
             resultdata.append(query.value("step").toString()+"-取放靶");
        }else if(query.value("type").toString()=="3"){
             resultdata.append(query.value("step").toString()+"-夹爪");
        }else if(query.value("type").toString()=="4"){
            resultdata.append(query.value("step").toString()+"-旋盖");
        }else if(query.value("type").toString()=="5"){
            resultdata.append(query.value("step").toString()+"-热室端送靶");
        }else if(query.value("type").toString()=="6"){
            resultdata.append(query.value("step").toString()+"-转液");
        }else if(query.value("type").toString()=="7"){
            resultdata.append(query.value("step").toString()+"-等待");
        }else if(query.value("type").toString()=="8"){
            resultdata.append(query.value("step").toString()+"-暂停");
        }else if(query.value("type").toString()=="9"){
            resultdata.append(query.value("step").toString()+"-联动");
        }else if(query.value("type").toString()=="10"){
            resultdata.append(query.value("step").toString()+"-活性检测");
        }else if(query.value("type").toString()=="11"){
            resultdata.append(query.value("step").toString()+"-旋盖夹爪");
        }
    }
    return resultdata;
}
/**
 * @brief 获取默认信息
 * @param 实验id
 * @return 是否默认 1是 0否
 */
int ExeConfig::zbgetdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select isdef From zbexe_config Where id='%1' and  step=0  ").arg(id);
    query.exec(sql);
    while (query.next()) {
        int a = query.value("isdef").toInt();
        return a;
    }
    return 0;
}
/**
 * @brief 设置默认流程
 * @param 实验id
 */
void ExeConfig::zbsetdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Update zbexe_config set isdef = 0 ";
    query.exec(sql);
    sql = QString("Update zbexe_config set isdef = 1 where id='%1' ").arg(id);
    query.exec(sql);
    emit updef();
    return;
}
/**
 * @brief 前端修改实验信息
 * @param 实验id
 * @param 实验名称
 * @param 耗材类型
 */
void ExeConfig::zbwsyxx(QString id,QString name,QString type){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    //    type=common->getItemCode("3",type);
    QString oldname="";
    QString sql = QString("select value_1 from zbexe_config where id='%1' and type=0 ").arg(id);
    query.exec(sql);
    while (query.next()){
        oldname.append(query.value("value_1").toString());
    }
    if(true){
        QString sql = QString("select value_1 from zbexe_config where id!='%4' and type=0 ").arg(id);
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==name){
                emit rename();
                return;
            }
        }
    }
    sql=QString("Update zbexe_config set value_1='%1',value_2='%2' Where id='%3' and type=0  ").arg(name,type,id);
    query.exec(sql);
    sql=QString("Update MainExperimentalProcess set ShiftingTargetExp='%1' Where ShiftingTargetExp='%2' ").arg(name,oldname);
    query.exec(sql);
    emit updatatab(id.toInt(),name,type,"");
    return;
}
/**
 * @brief 前端读实验名称
 * @param 实验id
 * @return 实验名称
 */
QStringList ExeConfig::zbrsyxx(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3 From zbexe_config Where id='%1' and  step=0 ").arg(id);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        res.append(query.value("value_1").toString());
        res.append(query.value("value_2").toString());
        res.append(query.value("value_3").toString());
    }
    if(res.isEmpty()){
        __LOG_ERROR("ResIsEmpty");
        return {};
    }
    return {res[0],res[1]};
}
/**
 * @brief 前端读取实验信息列表
 * @param 实验名称
 * @return 实验信息列表
 */
QJsonArray ExeConfig::zbreadsypzlist(QString name){
    if(name==""){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From zbexe_config Where step=0 order by id";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        if(syid.length()==0){
            __LOG_ERROR("SyidLength=0");
            return buttonDataArray;
        }
        if(syid.length()!=namelist.length()||syid.length()!=tyepelist.length()){
            __LOG_ERROR("LengthError");
            return buttonDataArray;
        }
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }else{
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From zbexe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        for(int i=syid.size()-1;i>=0;i--){
            if(!namelist[i].contains(name)){
                syid.removeAt(i);
                namelist.removeAt(i);
                tyepelist.removeAt(i);
            }
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        if(syid.length()==0){
            __LOG_ERROR("SyidLength=0");
            return buttonDataArray;
        }
        if(syid.length()!=namelist.length()||syid.length()!=tyepelist.length()){
            __LOG_ERROR("LengthError");
            return buttonDataArray;
        }
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }

}
/**
 * @brief 前端新建实验信息
 */
void ExeConfig::zbnewsypz(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select id From zbexe_config Where step=0 ";
    query.exec(sql);
    QList<int> syid;
    while (query.next()) {
        syid.append(query.value("id").toInt());
    }
    int newid=1;
    for(int i=1;i<100;i++){
        if(!syid.contains(i)){
            newid=i;
            break;
        }
    }
    //    int randomNumber = QRandomGenerator::global()->bounded(100);
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 定义日期时间格式
    QString format = "yyyyMMddhhmmss";
    // 将当前日期时间按照指定格式转换为字符串
    QString currentDateTimeString = currentDateTime.toString(format);
    QString newname = currentDateTimeString;
    if(true){
        QString sql = "select value_1 from zbexe_config where type=0 ";
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==newname){
                return;
            }
        }
    }
    sql="Select value From Deftab Where id=3 and deffl=1 ";
    query.exec(sql);
    QString deftype;
    while (query.next()) {
        deftype=query.value("value").toString();
    }
    deftype=common->getItemCode("3",deftype);
    sql=QString("INSERT INTO zbexe_config ( id, isdef, step, type, value_1, value_2 ) "
                " VALUES ( '%1', '%2', '%3', '%4', '%5', '%6' );").arg(QString::number(newid),"0","0","0",newname,"放靶");
    query.exec(sql);
    shunxunumb++;
    emit addlist();
    return;
}
/**
 * @brief 前端复制实验信息
 * @param 实验id
 */
void ExeConfig::zbcopyypz(QString copyid){
    if(copyid!="0"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id From zbexe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        while (query.next()) {
            syid.append(query.value("id").toInt());
        }
        int newid = 0;
        for(int i=1;i<100;i++){
            if(!syid.contains(i)){
                newid=i;
                break;
            }
        }
        sql=QString("Insert into zbexe_config select '%1',0,step,type,value_1,"
                    "value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 from zbexe_config where id = '%2' ").arg(QString::number(newid),copyid);
        query.exec(sql);
        // 获取当前日期和时间
        QDateTime currentDateTime = QDateTime::currentDateTime();
        // 定义日期时间格式
        QString format = "yyyyMMddhhmmss";
        // 将当前日期时间按照指定格式转换为字符串
        QString currentDateTimeString = currentDateTime.toString(format);
        QString newname = currentDateTimeString;
        sql=QString("Update zbexe_config set value_1='%1' Where id='%2' and type=0").arg(newname,QString::number(newid));
        query.exec(sql);
        emit addlist();
        return;
    }
}
/**
 * @brief 前端删除实验信息
 * @param 实验id
 */
void ExeConfig::zbdelsypz(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("select value_1 from zbexe_config where id='%1' and type=0 ").arg(id);
    query.exec(sql);
    QString syname="";
    while (query.next()){
        syname=query.value("value_1").toString();
    }
    QStringList sylcnamelist;
    sql = "select ShiftingTargetExp from MainExperimentalProcess  ";
    query.exec(sql);
    while (query.next()){
        sylcnamelist.append(query.value("ShiftingTargetExp").toString());
    }
    if(sylcnamelist.contains(syname)){
        emit delfailzb();
        return;
    }else{
        sql=QString("delete From zbexe_config Where id = '%1' ").arg(QString::number(id));
        query.exec(sql);
        sql = QString("UPDATE zbexe_config SET id = id - 1 WHERE id >'%1' ").arg(id);
        query.exec(sql);
        emit dellist();
        return;
    }
}
/**
 * @brief 前端更改信息
 * @param 实验id
 * @param 步骤id
 * @param 参数1-9
 */
void ExeConfig::zbupall(QString id,QString numv,QString v1,QString v2,QString v3,QString v4,QString v5,QString v6,QString v7,QString v8,QString v9){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update zbexe_config set value_1='%1',value_2='%2',value_3='%3',value_4='%4',value_5='%5',"
                        "value_6='%6',value_7='%7',value_8='%8',value_9='%9' Where id='%10' and step='%11' "
                        "").arg(v1,v2,v3,v4,v5,v6,v7,v8,v9,id,numv);
    query.exec(sql);
    sql = QString("Select type,value_1,value_2 From zbexe_config Where id='%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    QString kjlx,value2,value1;
    while (query.next()){
        kjlx=query.value("type").toString();
        value1=query.value("value_1").toString();
        value2=query.value("value_2").toString();
    }
    //    if(kjlx=="1"){
    //        sql=QString("Update zbexe_config set value_2='%1' Where id='%2' and type='%3' and value_1='%4' "
    //                    "").arg(value2,id,"1",value1);
    //        query.exec(sql);
    //    }
    emit uplist();
    emit save();
    return;
}
/**
 * @brief 前端读控件信息
 * @param 实验id
 * @param 步骤id
 * @return 步骤参数
 */
QStringList ExeConfig::zbreadall(QString id,QString numv){
    QStringList alldata;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 From zbexe_config Where id='%1' and  step='%2' ").arg(id,numv);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        alldata.append(query.value("value_1").toString());
        alldata.append(query.value("value_2").toString());
        alldata.append(query.value("value_3").toString());
        alldata.append(query.value("value_4").toString());
        alldata.append(query.value("value_5").toString());
        alldata.append(query.value("value_6").toString());
        alldata.append(query.value("value_7").toString());
        alldata.append(query.value("value_8").toString());
        alldata.append(query.value("value_9").toString());
    }
    return alldata;
}
/**
 * @brief 删除某控件
 * @param 实验id
 * @param 步骤id
 */
void ExeConfig::zbdelkj(QString id,QString numv){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    sql=QString("select value_3 From zbexe_config Where id = '%1' and type='7' and value_1='1' ").arg(id);
    query.exec(sql);
    QStringList syv3;
    while (query.next()){
        syv3.append(query.value("value_3").toString());
    }
    for(int i=0;i<syv3.length();i++){
        if(syv3[i]==numv){
            emit delv3();
            return;
        }
    }
    sql = QString("UPDATE zbexe_config SET value_3 = value_3 - 1 WHERE id = '%1' and value_3>'%2' and type='7' and value_1='1' ").arg(id,numv);
    query.exec(sql);
    sql=QString("delete From zbexe_config Where id = '%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE zbexe_config SET step = step - 1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    emit uplist();
}
/**
 * @brief 添加某控件
 * @param 实验id
 * @param 步骤id
 * @param 控件类型
 */
void ExeConfig::zbaddkj(QString id,QString numv,QString type){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    QString chname,chtype;
    if(type=="9"){
        sql="SELECT value_1,value_2 from exe_config where step=='0'";
        query.exec(sql);
        while (query.next()) {
            chname=query.value("value_1").toString();
            chtype=query.value("value_2").toString();
        }
        chtype=common->getItemName("3",chtype);
        if(chname.isEmpty()){
            emit chnoexp();
            return;
        }
    }
    sql = QString("UPDATE zbexe_config SET value_3 = value_3 +1 WHERE id = '%1' and value_3>'%2' and type='7' and value_1='1' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE zbexe_config SET step = step +1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    if(type=="1"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','遮挡',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="2"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','取靶',"
                    "'靶材罐','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="3"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','夹紧',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="4"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','开盖',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="5"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','开启',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="6"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','吸液',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="7"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','0',"
                    "'5','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="8"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','暂停',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="9"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','纯化',"
                    "'%4','%5','否','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv,chtype,chname);
    }else if(type=="10"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','上移',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="11"){
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','夹紧',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else{
        sql=QString("Insert into zbexe_config select id,isdef,step+1,'%1','',"
                    "'','','','','','','','' from zbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }
    query.exec(sql);
    emit uplist();
}
/**
 * @brief 控件移动添加
 * @param 实验id
 * @param 位置部署
 * @param 控件类型
 */
void ExeConfig::zbaddkjchange(QString id,double mbwz,QString type){
    int sjwz = mbwz-32;
    int sx = sjwz%33;
    int sjnum;
    if(sx>16.5){
        sjnum = sjwz/33+1;
    }else{
        sjnum = sjwz/33;
    }
    if(sjnum<=0){
        return;
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    int maxStep;
    QString sql=QString("Select max(step) as Step From zbexe_config Where id='%1'").arg(id);
    query.exec(sql);
    while (query.next()) {
        maxStep=query.value("step").toInt();
    }
    if(maxStep<sjnum){
        sjnum=maxStep+1;
    }
    QString chname,chtype;
    if(type=="9"){
        sql="SELECT value_1,value_2 from exe_config where step=='0'";
        query.exec(sql);
        while (query.next()) {
            chname=query.value("value_1").toString();
            chtype=query.value("value_2").toString();
        }
        chtype=common->getItemName("3",chtype);
        if(chname.isEmpty()){
            emit chnoexp();
            return;
        }
    }
    sql = QString("UPDATE zbexe_config SET step = step +1 WHERE id = '%1' and value_3>='%2' and type='7' and value_1='1' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql = QString("UPDATE zbexe_config SET step = step +1 WHERE id = '%1' and step>='%2' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql=QString("Select isdef From zbexe_config Where id='%1' and  step='0' ").arg(id);
    query.exec(sql);
    QString defv;
    while (query.next()){
        defv = query.value("isdef").toString();
    }
    if(type=="1"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"遮挡","","","","","","","","");
    }else if(type=="2"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"取靶","靶材罐","","","","","","","");
    }else if(type=="3"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"夹紧","","","","","","","","");
    }else if(type=="4"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"开盖","","","","","","","","");
    }else if(type=="5"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"开启","","","","","","","","");
    }else if(type=="6"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"吸液","","","","","","","","");
    }else if(type=="7"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"0","5","","","","","","","");
    }else if(type=="8"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"暂停","","","","","","","","");
    }else if(type=="9"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"纯化",chtype,chname,"否","","","","","");
    }else if(type=="10"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"上移","","","","","","","","");
    }else if(type=="11"){
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"夹紧","","","","","","","","");
    }else{
        sql=QString("INSERT INTO zbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"","","","","","","","","");
    }

    query.exec(sql);
    emit uplist();
}


/*
 * 打靶实验流程配置
 */
/**
 * @brief 数据库读某一实验流程
 * @param 实验id
 * @return 实验流程列表
 */
QJsonArray ExeConfig::dbreadsypz(int id)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type,value_1 From dbexe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList stepval;
    QStringList typeval;
    QStringList value_1val;
    QStringList colorval;
    QStringList pathval;
    QStringList idefval;
    //    stepval.append("");
    //    typeval.append("实验信息");
    //    value_1val.append("");
    //    colorval.append("#5BA55A");
    //    pathval.append("qrc:/picture/pz_xpkw.png");
    stepval.append("");
    typeval.append("开始");
    value_1val.append("");
    colorval.append("#BCA718");
    pathval.append("qrc:/picture/pz_ks.png");
    while (query.next()) {
        stepval.append(query.value("step").toString());
        if(query.value("type").toString()=="1"){
            typeval.append("吸取梭体");
            colorval.append("#00417E");
            pathval.append("qrc:/picture/pz_xb2.png");
        }else if(query.value("type").toString()=="2"){
            typeval.append("冷却");
            colorval.append("#5BA58C");
            pathval.append("qrc:/picture/pz_lq2.png");
        }else if(query.value("type").toString()=="3"){
            typeval.append("干燥");
            colorval.append("#BF5353");
            pathval.append("qrc:/picture/pz_gz2.png");
        }else if(query.value("type").toString()=="4"){
            typeval.append("梭体组合运行");
            colorval.append("#705BA5");
            pathval.append("qrc:/picture/pz_yb2.png");
        }else if(query.value("type").toString()=="5"){
            typeval.append("抱闸");
            colorval.append("#9B7E47");
            pathval.append("qrc:/picture/pz_sd2.png");
        }else if(query.value("type").toString()=="6"){
            typeval.append("梭体上靶");
            colorval.append("#5B66A4");
            pathval.append("qrc:/picture/pz_jl2.png");
        }else if(query.value("type").toString()=="7"){
            typeval.append("加速端回靶");
            colorval.append("#89439B");
            pathval.append("qrc:/picture/pz_cb2.png");
        }else if(query.value("type").toString()=="8"){
            typeval.append("等待");
            colorval.append("#FFB1D8");
            pathval.append("qrc:/picture/pz_dd.png");
        }else if(query.value("type").toString()=="9"){
            typeval.append("暂停");
            colorval.append("#89B5FF");
            pathval.append("qrc:/picture/pz_zt.png");
        }else if(query.value("type").toString()=="10"){
            typeval.append("联动");
            colorval.append("#7EE3D8");
            pathval.append("qrc:/picture/pz_ld.png");
        }
        value_1val.append(query.value("value_1").toString());
    }
    stepval.append("");
    typeval.append("结束");
    value_1val.append("");
    colorval.append("#A55B80");
    pathval.append("qrc:/picture/pz_js.png");
    QJsonArray buttonDataArray;
    QJsonObject rec;
    if(typeval.length()!=pathval.length()||typeval.length()!=colorval.length()
            ||typeval.length()!=value_1val.length()||typeval.length()!=stepval.length()){
        __LOG_ERROR("LengthError");
        return buttonDataArray;
    }
    for(int i = 0; i < stepval.size(); i++) {
        rec.insert("title", typeval[i]);
        rec.insert("icon", pathval[i]);
        rec.insert("color", colorval[i]);
        rec.insert("textval", value_1val[i]);
        rec.insert("numval", stepval[i]);
        buttonDataArray.append(rec);
    }
    return buttonDataArray;
}
/**
 * @brief 实验流程列表
 * @param 实验id
 */
QStringList ExeConfig::dbgetlclist(QString id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type From dbexe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList resultdata;
    while (query.next()) {
        if(query.value("type").toString()=="1"){
            resultdata.append(query.value("step").toString()+"-吸取梭体");
        }else if(query.value("type").toString()=="2"){
             resultdata.append(query.value("step").toString()+"-冷却");
        }else if(query.value("type").toString()=="3"){
             resultdata.append(query.value("step").toString()+"-干燥");
        }else if(query.value("type").toString()=="4"){
            resultdata.append(query.value("step").toString()+"-梭体组合运行");
        }else if(query.value("type").toString()=="5"){
            resultdata.append(query.value("step").toString()+"-抱闸");
        }else if(query.value("type").toString()=="6"){
            resultdata.append(query.value("step").toString()+"-梭体上靶");
        }else if(query.value("type").toString()=="7"){
            resultdata.append(query.value("step").toString()+"-加速端回靶");
        }else if(query.value("type").toString()=="8"){
            resultdata.append(query.value("step").toString()+"-等待");
        }else if(query.value("type").toString()=="9"){
            resultdata.append(query.value("step").toString()+"-暂停");
        }else if(query.value("type").toString()=="10"){
            resultdata.append(query.value("step").toString()+"-联动");
        }
    }
    return resultdata;
}
/**
 * @brief 获取默认信息
 * @param 实验id
 * @return 是否默认 1是 0否
 */
int ExeConfig::dbgetdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select isdef From dbexe_config Where id='%1' and  step=0  ").arg(id);
    query.exec(sql);
    while (query.next()) {
        int a = query.value("isdef").toInt();
        return a;
    }
    return 0;
}
/**
 * @brief 设置默认流程
 * @param 实验id
 */
void ExeConfig::dbsetdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Update dbexe_config set isdef = 0 ";
    query.exec(sql);
    sql = QString("Update dbexe_config set isdef = 1 where id='%1' ").arg(id);
    query.exec(sql);
    emit updef();
    return;
}
/**
 * @brief 前端修改实验信息
 * @param 实验id
 * @param 实验名称
 * @param 耗材类型
 */
void ExeConfig::dbwsyxx(QString id,QString name,QString type){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    //    type=common->getItemCode("3",type);
    QString oldname="";
    QString sql = QString("select value_1 from dbexe_config where id='%1' and type=0 ").arg(id);
    query.exec(sql);
    while (query.next()){
        oldname.append(query.value("value_1").toString());
    }
    if(true){
        QString sql = QString("select value_1 from dbexe_config where id!='%4' and type=0 ").arg(id);
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==name){
                emit rename();
                return;
            }
        }
    }
    sql=QString("Update dbexe_config set value_1='%1',value_2='%2' Where id='%3' and type=0  ").arg(name,type,id);
    query.exec(sql);
    sql=QString("Update MainExperimentalProcess set PracticeTargetExp='%1' Where PracticeTargetExp='%2' ").arg(name,oldname);
    query.exec(sql);
    emit updatatab(id.toInt(),name,type,"");
    return;
}
/**
 * @brief 前端读实验名称
 * @param 实验id
 * @return 实验名称
 */
QStringList ExeConfig::dbrsyxx(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3 From dbexe_config Where id='%1' and  step=0 ").arg(id);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        res.append(query.value("value_1").toString());
        res.append(query.value("value_2").toString());
        res.append(query.value("value_3").toString());
    }
    if(res.isEmpty()){
        __LOG_ERROR("resIsEmpty");
        return {};
    }
    return {res[0],res[1]};
}
/**
 * @brief 前端读取实验信息列表
 * @param 实验名称
 * @return 实验信息列表
 */
QJsonArray ExeConfig::dbreadsypzlist(QString name){
    if(name==""){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From dbexe_config Where step=0 order by id";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        if(syid.length()==0){
            __LOG_ERROR(syid);
            return buttonDataArray;
        }
        if(syid.length()!=namelist.length()||syid.length()!=tyepelist.length()){
            __LOG_ERROR(syid);
            return buttonDataArray;
        }
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }else{
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From dbexe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        for(int i=syid.size()-1;i>=0;i--){
            if(!namelist[i].contains(name)){
                syid.removeAt(i);
                namelist.removeAt(i);
                tyepelist.removeAt(i);
            }
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        if(syid.length()==0){
            return buttonDataArray;
        }
        if(syid.length()!=namelist.length()||syid.length()!=tyepelist.length()){
            return buttonDataArray;
        }
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }

}
/**
 * @brief 前端新建实验信息
 */
void ExeConfig::dbnewsypz(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select id From dbexe_config Where step=0 ";
    query.exec(sql);
    QList<int> syid;
    while (query.next()) {
        syid.append(query.value("id").toInt());
    }
    int newid=1;
    for(int i=1;i<100;i++){
        if(!syid.contains(i)){
            newid=i;
            break;
        }
    }
    //    int randomNumber = QRandomGenerator::global()->bounded(100);
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 定义日期时间格式
    QString format = "yyyyMMddhhmmss";
    // 将当前日期时间按照指定格式转换为字符串
    QString currentDateTimeString = currentDateTime.toString(format);
    QString newname = currentDateTimeString;
    if(true){
        QString sql = "select value_1 from dbexe_config where type=0 ";
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==newname){
                return;
            }
        }
    }
    sql="Select value From Deftab Where id=3 and deffl=1 ";
    query.exec(sql);
    QString deftype;
    while (query.next()) {
        deftype=query.value("value").toString();
    }
    deftype=common->getItemCode("3",deftype);
    sql=QString("INSERT INTO dbexe_config ( id, isdef, step, type, value_1, value_2 ) "
                " VALUES ( '%1', '%2', '%3', '%4', '%5', '%6' );").arg(QString::number(newid),"0","0","0",newname,"打靶");
    query.exec(sql);
    shunxunumb++;
    emit addlist();
    return;
}
/**
 * @brief 前端复制实验信息
 * @param 实验id
 */
void ExeConfig::dbcopyypz(QString copyid){
    if(copyid!="0"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id From dbexe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        while (query.next()) {
            syid.append(query.value("id").toInt());
        }
        int newid=1;
        for(int i=1;i<100;i++){
            if(!syid.contains(i)){
                newid=i;
                break;
            }
        }
        sql=QString("Insert into dbexe_config select '%1',0,step,type,value_1,"
                    "value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 from dbexe_config where id = '%2' ").arg(QString::number(newid),copyid);
        query.exec(sql);
        // 获取当前日期和时间
        QDateTime currentDateTime = QDateTime::currentDateTime();
        // 定义日期时间格式
        QString format = "yyyyMMddhhmmss";
        // 将当前日期时间按照指定格式转换为字符串
        QString currentDateTimeString = currentDateTime.toString(format);
        QString newname = currentDateTimeString;
        sql=QString("Update dbexe_config set value_1='%1' Where id='%2' and type=0").arg(newname,QString::number(newid));
        query.exec(sql);
        emit addlist();
        return;
    }
}
/**
 * @brief 前端删除实验信息
 * @param 实验id
 */
void ExeConfig::dbdelsypz(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("select value_1 from dbexe_config where id='%1' and type=0 ").arg(id);
    query.exec(sql);
    QString syname="";
    while (query.next()){
        syname=query.value("value_1").toString();
    }
    QStringList sylcnamelist;
    sql = "select PracticeTargetExp from MainExperimentalProcess  ";
    query.exec(sql);
    while (query.next()){
        sylcnamelist.append(query.value("PracticeTargetExp").toString());
    }
    if(sylcnamelist.contains(syname)){
        emit delfaildb();
        return;
    }else{
        sql=QString("delete From dbexe_config Where id = '%1' ").arg(QString::number(id));
        query.exec(sql);
        sql = QString("UPDATE dbexe_config SET id = id - 1 WHERE id >'%1' ").arg(id);
        query.exec(sql);
        emit dellist();
        return;
    }
}
/**
 * @brief 前端更改信息
 * @param 实验id
 * @param 步骤id
 * @param 参数1-9
 */
void ExeConfig::dbupall(QString id,QString numv,QString v1,QString v2,QString v3,QString v4,QString v5,QString v6,QString v7,QString v8,QString v9){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update dbexe_config set value_1='%1',value_2='%2',value_3='%3',value_4='%4',value_5='%5',"
                        "value_6='%6',value_7='%7',value_8='%8',value_9='%9' Where id='%10' and step='%11' "
                        "").arg(v1,v2,v3,v4,v5,v6,v7,v8,v9,id,numv);
    query.exec(sql);
    sql = QString("Select type,value_1,value_2 From dbexe_config Where id='%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    QString kjlx,value2,value1;
    while (query.next()){
        kjlx=query.value("type").toString();
        value1=query.value("value_1").toString();
        value2=query.value("value_2").toString();
    }
    //    if(kjlx=="1"){
    //        sql=QString("Update dbexe_config set value_2='%1' Where id='%2' and type='%3' and value_1='%4' "
    //                    "").arg(value2,id,"1",value1);
    //        query.exec(sql);
    //    }
    emit uplist();
    emit save();
    return;
}
/**
 * @brief 前端读控件信息
 * @param 实验id
 * @param 步骤id
 * @return 步骤参数
 */
QStringList ExeConfig::dbreadall(QString id,QString numv){
    QStringList alldata;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 From dbexe_config Where id='%1' and  step='%2' ").arg(id,numv);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        alldata.append(query.value("value_1").toString());
        alldata.append(query.value("value_2").toString());
        alldata.append(query.value("value_3").toString());
        alldata.append(query.value("value_4").toString());
        alldata.append(query.value("value_5").toString());
        alldata.append(query.value("value_6").toString());
        alldata.append(query.value("value_7").toString());
        alldata.append(query.value("value_8").toString());
        alldata.append(query.value("value_9").toString());
    }
    return alldata;
}
/**
 * @brief 删除某控件
 * @param 实验id
 * @param 步骤id
 */
void ExeConfig::dbdelkj(QString id,QString numv){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    sql=QString("select value_3 From dbexe_config Where id = '%1' and type='8' and value_1='1' ").arg(id);
    query.exec(sql);
    QStringList syv3;
    while (query.next()){
        syv3.append(query.value("value_3").toString());
    }
    for(int i=0;i<syv3.length();i++){
        if(syv3[i]==numv){
            emit delv3();
            return;
        }
    }
    sql = QString("UPDATE dbexe_config SET value_3 = value_3 - 1 WHERE id = '%1' and value_3>'%2' and type='8' and value_1='1' ").arg(id,numv);
    query.exec(sql);
    sql=QString("delete From dbexe_config Where id = '%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE dbexe_config SET step = step - 1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    emit uplist();
}
/**
 * @brief 添加某控件
 * @param 实验id
 * @param 步骤id
 * @param 控件类型
 */
void ExeConfig::dbaddkj(QString id,QString numv,QString type){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql;
    QString chname,chtype;
    if(type=="10"){
        sql="SELECT value_1,value_2 from exe_config where step=='0'";
        query.exec(sql);
        while (query.next()) {
            chname=query.value("value_1").toString();
            chtype=query.value("value_2").toString();
        }
        chtype=common->getItemName("3",chtype);
        if(chname.isEmpty()){
            emit chnoexp();
            return;
        }
    }
    sql = QString("UPDATE dbexe_config SET value_3 = value_3 +1 WHERE id = '%1' and value_3>'%2' and type='8' and value_1='1' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE dbexe_config SET step = step +1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    if(type=="1"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','开启',"
                    "'','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="2"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','',"
                    "'','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="3"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','',"
                    "'','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="4"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','进靶',"
                    "'','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="5"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','锁止',"
                    "'','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="6"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','下移',"
                    "'','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="7"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','加速端回靶',"
                    "'','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="8"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','0',"
                    "'5','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="9"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','暂停',"
                    "'','','','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
        query.exec(sql);
    }else if(type=="10"){
        sql=QString("Insert into dbexe_config select id,isdef,step+1,'%1','纯化',"
                    "'%4','%5','否','','','','','' from dbexe_config where id = '%2' and step='%3'  ").arg(type,id,numv,chtype,chname);
        query.exec(sql);
    }
    emit uplist();
}
/**
 * @brief 控件移动添加
 * @param 实验id
 * @param 位置部署
 * @param 控件类型
 */
void ExeConfig::dbaddkjchange(QString id,double mbwz,QString type){
    int sjwz = mbwz-32;
    int sx = sjwz%33;
    int sjnum;
    if(sx>16.5){
        sjnum = sjwz/33+1;
    }else{
        sjnum = sjwz/33;
    }
    if(sjnum<=0){
        return;
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    int maxStep;
    QString sql=QString("Select max(step) as Step From dbexe_config Where id='%1'").arg(id);
    query.exec(sql);
    while (query.next()) {
        maxStep=query.value("step").toInt();
    }
    if(maxStep<sjnum){
        sjnum=maxStep+1;
    }
    QString chname,chtype;
    if(type=="10"){
        sql="SELECT value_1,value_2 from exe_config where step=='0'";
        query.exec(sql);
        while (query.next()) {
            chname=query.value("value_1").toString();
            chtype=query.value("value_2").toString();
        }
        chtype=common->getItemName("3",chtype);
        if(chname.isEmpty()){
            emit chnoexp();
            return;
        }
    }
    sql = QString("UPDATE dbexe_config SET step = step +1 WHERE id = '%1' and value_3>='%2' and type='8' and value_1='1' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql = QString("UPDATE dbexe_config SET step = step +1 WHERE id = '%1' and step>='%2' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql=QString("Select isdef From dbexe_config Where id='%1' and  step='0' ").arg(id);
    query.exec(sql);
    QString defv;
    while (query.next()){
        defv = query.value("isdef").toString();
    }
    if(type=="1"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"开启","","","","","","","","");
    }else if(type=="2"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"","","","","","","","","");
    }else if(type=="3"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"","","","","","","","","");
    }else if(type=="4"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"进靶","","","","","","","","");
    }else if(type=="5"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"锁止","","","","","","","","");
    }else if(type=="6"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"下移","","","","","","","","");
    }else if(type=="7"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"加速端回靶","","","","","","","","");
    }else if(type=="8"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"0","5","","","","","","","");
    }else if(type=="9"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"暂停","","","","","","","","");
    }else if(type=="10"){
        sql=QString("INSERT INTO dbexe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"纯化",chtype,chname,"否","","","","","");
    }

    query.exec(sql);
    emit uplist();
}
/**
 * @brief 实验流程配置前端读取
 * @return 实验流程配置列表
 */
Q_INVOKABLE QVariantList ExeConfig::getAllEx(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select * from MainExperimentalProcess ";
    query.exec(sql);
    QVariantList result;
    while (query.next())
    {
        QStringList info;
        for (int i = 0; i < query.record().count(); ++i){
            info.append(query.value(i).toString());
        }
        result.append(info);
    }
    return result;
}
/**
 * @brief 实验流程配置全部删除
 */
Q_INVOKABLE void ExeConfig::delAllEx(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="delete from MainExperimentalProcess ";
    query.exec(sql);
}
/**
 * @brief 实验流程配置删除某个配置文件
 * @param 实验流程id
 */
void ExeConfig::delExbyid(QString id)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("delete from MainExperimentalProcess where Serial='%1' ").arg(id);
    query.exec(sql);
    sql = QString("UPDATE MainExperimentalProcess SET Serial = Serial - 1 WHERE Serial >'%1' ").arg(id);
    query.exec(sql);
}

/**
 * @brief 实验流程配置全部保存
 */
Q_INVOKABLE void ExeConfig::saveAllEx(QVariantList alldata){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="DELETE FROM MainExperimentalProcess ";
    query.exec(sql);
    foreach(QVariant syning,alldata){
        QStringList map=syning.value<QStringList>();
        if(map.size() < 7)    { __LOG_ERROR(map);  return;  }       // 校验
        sql=QString("Insert into MainExperimentalProcess Values('%1','%2','%3','%4',"
                    "'%5','%6','%7')").arg(map[0],map[1],map[2],map[3],map[4],map[5],map[6]);
        query.exec(sql);
    }
}
/**
 * @brief 获取内径
 * @param 注射器类型
 * @return 内径
 */
Q_INVOKABLE QString ExeConfig::getnj(QString zsqtype){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("select value_2 from Deftab  where id='1' and value='%1' ").arg(zsqtype);
    query.exec(sql);
    if(query.next()){
        return query.value("value_2").toString();
    }
    return NULL;
}

/**
 * @brief 获取屏蔽信息
 * @return 屏蔽列表
 */
QStringList ExeConfig::getMaskingInformation(){
    QStringList result;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='4' and num='1' ";
    query.exec(sql);
    while (query.next()) {
        result.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='4' and num='2' ";
    query.exec(sql);
    while (query.next()) {
        result.append(query.value("value").toString());
    }


    if(result.size() < 2)    { __LOG_ERROR(result);  return result;  }       // 校验

    if(result[0]=="1"&& result[1]=="1"){
        StaticProperties::softType=0;
    }
    else if(result[0]=="1"&&result[1]=="0"){
        StaticProperties::softType=1;
    }
    else if(result[0]=="0" && result[1]=="1"){
        StaticProperties::softType=2;
    }
    return result;
}
/**
 * @brief 写入屏蔽信息
 * @param 屏蔽信息列表
 */
void ExeConfig::updataMaskingInformation(QStringList value){

    if(value.size() < 2)    { __LOG_ERROR(value);  return;  }       // 校验

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update Deftab set value='%1' where id='4' and num='1' "
                        "").arg(value[0]);
    query.exec(sql);
    sql=QString("Update Deftab set value='%1' where id='4' and num='2' "
                "").arg(value[1]);
    query.exec(sql);
    emit maskingInformation(value);
    return;
}
